package com.dingup.api.template.message.bean.msg;

import org.apache.commons.lang.StringUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Created by wancheng on 2018/10/17.
 */
public class MarkdownUtil {

    public static String getTitle(String title, Integer level) {
        String templet = "%s %s";
        StringBuffer sign = new StringBuffer("");
        for (int i = 0; i < level; i++) {
            sign.append("#");
        }
        return String.format(templet, sign.toString(), title);
    }

    public static String getQuote(String content) {
        StringBuffer sign = new StringBuffer("> ");
        sign.append(content);
        return sign.toString();
    }

    public static String getFontWeight(String content) {
        String templet = "%s%s%s";
        return String.format(templet, "**", content, "**");
    }

    public static String getLink(String link, String description) {
        StringBuffer sign = new StringBuffer("");
        sign.append("[");
        sign.append(description);
        sign.append("]");
        sign.append("(");
        sign.append(link);
        sign.append(")");
        return sign.toString();
    }

    public static String getImage(String image) {
        if (!StringUtils.isEmpty(image)){
            StringBuffer sign = new StringBuffer("");
            sign.append("![]");
            sign.append("(");
            sign.append(image);
            sign.append(")");
            return sign.toString();
        }
        return "";
    }

    public static String getList(String[] array, Boolean ifSort) {
        List<String> list = Arrays.asList(array);
        List<String> res = new ArrayList<>();
        for (int i = 0; i < list.size() ; i++) {
            StringBuffer sb = new StringBuffer("");
            if (ifSort){
                sb.append(i+1+". ");
            }else{
                sb.append("- ");
            }
            sb.append(list.get(i));
            res.add(sb.toString());
        }
        return res.stream().collect(Collectors.joining("\n"));
    }

    public static String getMarkdown(String... array){
        List<String> arrayList = Arrays.asList(array);
        return arrayList.stream().collect(Collectors.joining("\n"));
    }
}
